<?php

// $Id$


require_once(TANGRA_MAIN_DIR.'interfaces/i_db_storable.class.php');


require_once('tcms_nomenclature.class.php');


class TCMS_Nomenclature_DBC extends TCMS_Nomenclature implements I_DB_Storable {
	public function save(DB_Connection $dbc) {
		$ret = false;

		if ($this->get_id()) {
			$ret = $this->update($dbc);
		} else {
			$ret = $this->insert($dbc);
			$this->set_id($ret);

		}

		return $ret;
	}


	public function load_by_id(DB_Connection $dbc, $id) {
		tangra_if_not_int_throw_e($id);

		$ret = false;

		$sql = "select hid, ".
						"name ".
					"from tcms_nomenclatures where id = $id";
		$rez = $dbc->execute($sql);

		if (!$rez->is_eof()) {
			$rez_obj = $rez->fetch_object();

			$this->set_id($id);
			$this->set_hid(stripslashes($rez_obj->HID));
			$this->set_name(stripslashes($rez_obj->NAME));

			$ret = $id;
		}

		return $ret;
	}


	protected function insert(DB_Connection $dbc) {
		$ret = false;

		$id = $dbc->generate_id('tcms_nomenclatures_seq');
		$hid = addslashes($this->get_hid());
		$name = addslashes($this->get_name());

		if ($id) {
			$sql = "insert into tcms_nomenclatures (id, ".
									      "hid, ".
									      "name ".
									      ") ".
								"values ".
									      "($id,".
									      "'$hid', ".
									      "'$name' ".
									      ")";
			$dbc->execute($sql);
			$ret = $id;
		} else {
			throw new TE_Exception('tcms_nomenclatures_seq');
		}

		return $ret;
	}


	protected function update(DB_Connection $dbc) {
		$id = $this->get_id();

		$hid = addslashes($this->get_hid());
		$name = addslashes($this->get_name());

		$sql = "update tcms_nomenclatures set ".
						"hid = '$hid', ".
						"name = '$name' ".
					"where id = ".$id;

		$dbc->execute($sql);
		$ret = $id;

		return $ret;
	}


	public static function delete(DB_Connection $dbc, $id) {
		tangra_if_not_int_throw_e($id);

		$sql = "delete from tcms_nomenclatures where id = $id";
		$dbc->execute($sql);
	}


	public static function get_sql_for_grid() {
		$sql = "select id, ".
						"hid, ".
						"name ".
					"from tcms_nomenclatures order by hid asc";

		return $sql;
	}


	public static function	get_sql_count_for_grid() {
		$sql = "select count(id) as total_rows from tcms_nomenclatures";

		return $sql;
	}


	public static function load_types_ol_map(DB_Connection $dbc) {
		$sql = "select id, name from tcms_nomenclatures order by name asc";
		$rez = $dbc->execute($sql);

		$ret = array();
		$ids = array();

		while($rez_obj = $rez->fetch_object()) {
			array_push($ids, $rez_obj->ID);
			$ret['ol_map'][$rez_obj->ID] = $rez_obj->NAME;
		}

		if ($ids) {
			$ret['ids'] = $ids;
		}

		return $ret;
	}


	public static function is_unique_hid(DB_Connection $dbc, $id, $hid) {
		tangra_if_not_int_throw_e($id);
		$hid = addslashes($hid);

		$sql = "select id from tcms_nomenclatures where id <> $id and hid = '$hid'";
		$rez = $dbc->execute($sql);

		return $rez->is_eod();
	}


	public function load_by_hid(DB_Connection $dbc, $hid) {
		$hid = addslashes($hid);
		$ret = false;

		$sql = "select id from tcms_nomenclatures where hid = '$hid'";
		$rez = $dbc->execute($sql);
		if (!$rez->is_eod()) {
			$rez_obj = $rez->fetch_object();
			$ret = $this->load_by_id($dbc, $rez_obj->ID);
		}

		return $ret;
	}
}
